<?php
/**
 * 汇友科技
 * @copyright (c) 2017 http://www.huiyou365.net All rights reserved.
 * @author: Creatint <creatint@163.com>
 */

namespace Home\Model;

use Common\Model\Model;
use Think\Page;

class ArticleModel extends Model{
    protected $error = null;
    private $page = null;

    public function position($pos = null, $limit = null, $field = null, $status = 1)
    {

        // 查询条件
        $map = $this->map($status, $pos);

        // 设置列表数量
        is_numeric($limit) && $this->limit($limit);

        // 进行查询
        $this->lists = $this
            ->where($map)
            ->order('updated_at DESC')
            ->select();

        return $this;
    }

    public function next( $info )
    {
        $map = array(
            'status'    =>  1,
            'updated_at'  =>  array('lt', $info['updated_at'])
        );
        $res = $this->where($map)->order('updated_at DESC')->find();
        return $res;
    }
    public function prev( $info )
    {
        $map = array(
            'status'    =>  1,
            'updated_at'  =>  array('gt', $info['updated_at'])
        );
        $res = $this->where($map)->order('updated_at DESC')->find();
        return $res;
    }

    public function map($status = 1, $pos = null){
        $map = array();

        // 状态
        $map['status']  =   $status;

        // 推荐位
        if ( $pos >0 )
            $map[]  =   "position & {$pos} = {$pos}";

        return $map;
    }

    public function getLists(){
        return $this->lists;
    }

    public function getPage(){
        return $this->page;
    }

    public function getError(){
        return $this->error;
    }
}